var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Goods = require('../models/goods');

mongoose.connect('mongodb://127.0.0.1:27017/vuemmall');
mongoose.connection.on("connected",function(){
    console.log("连接成功")
});
mongoose.connection.on('error', function () {
    console.log("连接失败")
});
mongoose.connection.on('disconnected', function () {
    console.log("disconnected")
});
// 筛选商品列表
router.get("/list", function (req,res,next) {
    // 通过req.param获取url里的参数，get取参方法
    let page = parseInt(req.param('page'))
    let pageSize = parseInt(req.param('pageSize'))
    let priceLevel = req.param('priceLevel')
    let sort = req.param("sort")
    let skip = (page-1) * pageSize
    var priceGt = '',priceLte = ''
    let params = {}
    if(priceLevel != 'all') {
        switch(priceLevel) {
            case '0':priceGt = 0;priceLte=100;break;
            case '1':priceGt = 100;priceLte=500;break;
            case '2':priceGt = 500;priceLte=1000;break;
            case '3':priceGt = 1000;priceLte=5000;break;
        }
        params = {
            salePrice: {
                $gt:priceGt,
                $lte:priceLte
            }
        }
    }
    //skip()跳过多少条数据，limit()展示多少条数据
    let goodsModel = Goods.find(params).skip(skip).limit(pageSize)
    goodsModel.sort({'salePrice':sort})//sort()里面必须是一个对象
    goodsModel.exec(function (err,doc) {
        if(err) {
            res.json({
                status:'1',
                msg:err.message
            });
        }else{
            res.json({
                status:'0',
                msg:'',
                result:{
                    count:doc.length,
                    list:doc
                }
            });
        }
    })
});
// 加入购物车
router.post('/addCart', function (req,res,next) {
    // post取参通过req.body取
    var userId = req.cookies.userId,productId = req.body.productId
    var User = require('../models/users')
    User.findOne({userId:userId}, function (err,userDoc) {
        if(err) {
            res.json({
                status: '1',
                msg: err.message
            })
        }else{
            // 已经拿到用户了
            if(userDoc) {
                // 加入重复商品时直接加商品数量
                let goodsItem = ''
                userDoc.cartList.forEach(function(item){
                    if(item.productId == productId){
                        goodsItem = item
                        item.productNum ++
                    }
                })
                // 做一下保存
                if(goodsItem){
                    userDoc.save(function (err2,doc2) {
                        if(err2) {
                            res.json({
                                status: '1',
                                test:'err11',
                                msg: err2.message
                            })
                        }else{
                            res.json({
                                status: '0',
                                msg: '',
                                result: 'suc'
                            })
                        }
                    })
                }else{
                    Goods.findOne({productId:productId}, function (err1,doc){
                        if(err1){
                            res.json({
                                status:'1',
                                test:'err1',
                                msg:err1.message
                            })
                        }else{
                            if(doc) {
                                doc.productNum = 1
                                doc.checked = 1
                                userDoc.cartList.push(doc)
                                userDoc.save(function (err2,doc2) {
                                    if(err2) {
                                        res.json({
                                            status: '1',
                                            msg: err2.message,
                                            test:'err2'
                                        })
                                    }else{
                                        res.json({
                                            status: '0',
                                            msg: '加入成功',
                                            result: 'success'
                                        })
                                    }
                                })
    
                            }
                        }
                    })
                }
            }
        }
    })
})
module.exports = router;